﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common_Code.Math_Extensions
{
    public class PolyFunction
    {
        private double[][] func;

        PolyFunction()
        {
            func = null;
        }

        PolyFunction(double[][] func)
        {
            SetFunction(func);
        }

        public void SetFunction(double[][] func)
        {
            if (func[0].Length > 2)
                throw new ArgumentException("This array does not match the correct format");
            this.func = func;
        }

        public double Eval(double x)
        {
            if (func == null || func.Length == 0)
                throw new ArgumentException("There is no function to evaluate");
            double val = 0;
            for (var i = (func.Length - 1); i >= 0; i--)
            {
                if (func[0].Length > 1)
                    val += Math.Pow(x, func[i][1]) * func[i][0];
                else
                    val += Math.Pow(x, i) * func[i][0];
            }
            return val;
        }
    }
}
